#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <ncs_rpg>

#define VERSION		"BETA TEST"

new ThisSkillID;
new Float:SkillChance, SkillAngles;

public Plugin:myinfo =
{
	name		= "[NCS RPG] Skill Position Swap",
	author		= "[RUS] DiviX",
	description	= "Skill Position Swap for NCS RPG",
	version		= VERSION,
	url			= "N/A"
};

public OnPluginStart()
{
	HookEvent("player_hurt", Event_PlayerHurt);
}

public RPG_OnSkillsRegister()
{
	ThisSkillID = RegisterSkill("pswap", 5, 20, 15);
	SkillChance = GetConfigValueFloat("pswap", "chance", 1.0)*0.01;
	SkillAngles = GetConfigValue("pswap", "view", 1);
}

public Event_PlayerHurt(Handle:event, const String:name[], bool:dontBroadcast)
{
	new victim = GetClientOfUserId(GetEventInt(event, "userid"));
	new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
	if(IsValidPlayer(victim, true) && IsValidPlayer(attacker, true) && attacker != victim)
	{
		new level = GetSkillLevel(victim, ThisSkillID);
		if(level > 0)
			if(GetRandomFloat() <= SkillChance*level)
			{
				decl Float:pos_victim[3], Float:pos_attacker[3];
				GetClientAbsOrigin(victim, pos_victim);
				GetClientAbsOrigin(attacker, pos_attacker);
				if(SkillAngles)
				{
					decl Float:angles[3];
					GetClientAbsAngles(attacker, angles);
					TeleportEntity(victim, pos_attacker, angles, NULL_VECTOR);
				}
				else
					TeleportEntity(victim, pos_attacker, NULL_VECTOR, NULL_VECTOR);
				TeleportEntity(attacker, pos_victim, NULL_VECTOR, NULL_VECTOR);
			}
	}
}